package com.winai.launcher.view.timeManager;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.recyclerview.widget.GridLayoutManager;

import com.github.jdsjlzx.recyclerview.LRecyclerView;
import com.github.jdsjlzx.recyclerview.LRecyclerViewAdapter;
import com.winai.launcher.R;
import com.winai.launcher.base.BaseActivity;
import com.winai.launcher.base.BaseRecAdapter;
import com.winai.launcher.base.BaseRecViewHolder;
import com.winai.launcher.bean.WeekRepeatBean;
import com.winai.launcher.utils.PicassoLoadUtils;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 作者：康少
 * 时间：2019/12/13 0013
 * 说明：闹钟重复
 */
public class AlarmRepeatActivity extends BaseActivity {
    private Context mContext = this;

    @BindView(R.id.str_title)
    TextView strTitle;
    @BindView(R.id.recyclerView)
    LRecyclerView recyclerView;

    private DayRepeatAdapter dayRepeatAdapter;
    List<WeekRepeatBean> weekRepeatBeanList = new ArrayList<>();
    List<WeekRepeatBean> defaultRepeatBeanList = new ArrayList<>();
    //选中的星期——逗号隔开的字符串
    private String repeat;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_alarm_repeat);
        ButterKnife.bind(this);
        start();
    }

    private void start() {
        initObject();
        addData();
    }

    private void initObject() {
        strTitle.setText("编辑闹钟");
        repeat = getIntent().getStringExtra("repeat");
        if (repeat != null) {
            if (repeat.equals("每天")) {
                defaultRepeatBeanList.add(new WeekRepeatBean("星期日", true));
                defaultRepeatBeanList.add(new WeekRepeatBean("星期一", true));
                defaultRepeatBeanList.add(new WeekRepeatBean("星期二", true));
                defaultRepeatBeanList.add(new WeekRepeatBean("星期三", true));
                defaultRepeatBeanList.add(new WeekRepeatBean("星期四", true));
                defaultRepeatBeanList.add(new WeekRepeatBean("星期五", true));
                defaultRepeatBeanList.add(new WeekRepeatBean("星期六", true));
            } else if (repeat.equals("工作日")) {
                defaultRepeatBeanList.add(new WeekRepeatBean("星期一", true));
                defaultRepeatBeanList.add(new WeekRepeatBean("星期二", true));
                defaultRepeatBeanList.add(new WeekRepeatBean("星期三", true));
                defaultRepeatBeanList.add(new WeekRepeatBean("星期四", true));
                defaultRepeatBeanList.add(new WeekRepeatBean("星期五", true));
            } else {
                String[] split = repeat.split("，");
                for (int i = 0; i < split.length; i++) {
                    defaultRepeatBeanList.add(new WeekRepeatBean(split[i], true));
                }
            }
        }

        dayRepeatAdapter = new DayRepeatAdapter();
        LRecyclerViewAdapter lRecyclerViewAdapter = new LRecyclerViewAdapter(dayRepeatAdapter);
        recyclerView.setLayoutManager(new GridLayoutManager(mContext, 1));
        recyclerView.setAdapter(lRecyclerViewAdapter);
        recyclerView.setPullRefreshEnabled(false);
        recyclerView.setLoadMoreEnabled(false);
    }

    private void addData() {
        weekRepeatBeanList.clear();
        weekRepeatBeanList.add(new WeekRepeatBean("星期日", false));
        weekRepeatBeanList.add(new WeekRepeatBean("星期一", false));
        weekRepeatBeanList.add(new WeekRepeatBean("星期二", false));
        weekRepeatBeanList.add(new WeekRepeatBean("星期三", false));
        weekRepeatBeanList.add(new WeekRepeatBean("星期四", false));
        weekRepeatBeanList.add(new WeekRepeatBean("星期五", false));
        weekRepeatBeanList.add(new WeekRepeatBean("星期六", false));
        for (int i = 0; i < defaultRepeatBeanList.size(); i++) {
            for (int j = 0; j < weekRepeatBeanList.size(); j++) {
                if (defaultRepeatBeanList.get(i).getTvName().equals(weekRepeatBeanList.get(j).getTvName())) {
                    weekRepeatBeanList.get(j).setChecked(true);
                    break;
                }
            }
        }
        new Handler(Looper.getMainLooper()).post(new Runnable() {
            @Override
            public void run() {
                dayRepeatAdapter.refreshData(weekRepeatBeanList);
            }
        });
    }

    @OnClick({R.id.iv_back, R.id.iv_home})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.iv_back:
                Intent intent = new Intent();
                intent.putExtra("repeat", repeat);
                setResult(Activity.RESULT_OK, intent);
                finish();
                break;
            case R.id.iv_home:
                //模拟HOME
                Intent it = new Intent(Intent.ACTION_MAIN);
                it.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                it.addCategory(Intent.CATEGORY_HOME);
                mContext.startActivity(it);
                break;
        }
    }

    private class DayRepeatAdapter extends BaseRecAdapter<WeekRepeatBean, DayRepeatAdapter.ViewHolder> {
        @Override
        public void onHolder(DayRepeatAdapter.ViewHolder holder, WeekRepeatBean bean, int position) {
            TextView tvDay = holder.getView(R.id.tv_day);
            ImageView ivCheck = holder.getView(R.id.iv_checked);
            tvDay.setText(bean.getTvName());
            if (bean.isChecked()) {
                PicassoLoadUtils.getInstance().picassoLoadNoPlaceholder(R.mipmap.icon_tag_checked, ivCheck);
            } else {
                PicassoLoadUtils.getInstance().picassoLoadNoPlaceholder(R.mipmap.icon_tag_unchecked, ivCheck);
            }
            holder.itemView.setOnClickListener(v -> {
                bean.setChecked(!bean.isChecked());
                repeat = formatRepeat(getData());
                //更新视图
                notifyDataSetChanged();
            });
        }

        @Override
        public DayRepeatAdapter.ViewHolder onCreateHolder() {
            return new ViewHolder(getViewByRes(R.layout.item_day_repeat));
        }

        private class ViewHolder extends BaseRecViewHolder {
            ViewHolder(View itemView) {
                super(itemView);
            }
        }
    }

    /**
     * 说明：格式化列表 为字符串显示
     */
    private String formatRepeat(List<WeekRepeatBean> defaultRepeat) {
        boolean allChecked = true;
        boolean allUnChecked = true;
        for (WeekRepeatBean dd : defaultRepeat) {
            if (!dd.isChecked()) {
                allChecked = false;
                break;
            }
        }
        for (WeekRepeatBean dd : defaultRepeat) {
            if (dd.isChecked()) {
                allUnChecked = false;
                break;
            }
        }
        if (allChecked) {
            return "每天";
        } else if (allUnChecked) {
            return "永不";
        } else {
            //选出选中的item
            List<String> checkedBean = new ArrayList<>();
            for (WeekRepeatBean bb : defaultRepeat) {
                if (bb.isChecked()) {
                    checkedBean.add(bb.getTvName());
                }
            }
            //将选中的item转换成一个字符串，并用逗号隔开
            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < checkedBean.size(); i++) {
                if (i != 0) {
                    sb.append("，");
                }
                sb.append(checkedBean.get(i));
            }
            String newStr = sb.toString();
            //判断是否选中了工作日
            if (newStr.contains("一") && newStr.contains("二") && newStr.contains("三")
                    && newStr.contains("四") && newStr.contains("五") && !newStr.contains("六")
                    && !newStr.contains("日")) {
                return "工作日";
            } else {
                return newStr;
            }
        }
    }
}
